package com.example.demo.security.sysGroup.service;


import com.example.demo.security.sysGroup.dao.GroupDao;
import com.example.demo.security.sysGroup.entity.GroupBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GroupService {

    @Autowired
    private GroupDao groupDao;

    public List<GroupBean> queryGroups(Integer userId){
        return groupDao.queryGroups(userId);
    }

    public String queryGroupNameById(Integer id){
        GroupBean groupBean=groupDao.queryGroupNameById(id);
        return groupBean.getGroupName();
    }

    public Integer addOneGroupMessage(Integer groupId ,Integer id){
        return groupDao.addGroupMessage(groupId,id);
    }

    public Integer addGroupMessage(Integer groupId ,Integer[] userIds){
        Integer i=0;
        for (Integer id:userIds){
            i+=groupDao.addGroupMessage(groupId,id);
        }
        return i;
    }

    public Integer addGroup(String groupName,Integer id){
        GroupBean groupBean=new GroupBean();
        groupBean.setGroupName(groupName);
        groupBean.setUserId(id);
        return groupDao.insertAndGetId(groupBean);
    }
    public Integer exitGroup(Integer id,Integer groupId){
        Integer i=0;
        Integer count=groupDao.queryPeople(groupId);
        if (count<=1){
            groupDao.removeGroup(groupId);
        }
        i+=groupDao.delGroupMessage(id,groupId);
        i+=groupDao.removeInGroup(id,groupId);
        return i;
    }
}
